package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.pojo.SetmealDto;
import cn.usian.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //分页查询
    @GetMapping("page")
    public R findPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer pageSize,String name){
        return setmealService.findPage(page,pageSize,name);
    }

    //添加
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDTO, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        setmealDTO.setCreateUser(id);
        setmealDTO.setUpdateUser(id);
        return setmealService.save(setmealDTO);
    }
    //修改回显
    @GetMapping("{id}")
    public R findById(@PathVariable("id") Long id){
        return setmealService.findById(id);
    }


    @PutMapping
    public R edit(@RequestBody SetmealDto setmealDTO,HttpSession session){
        Long id = (Long) session.getAttribute("emp");
        setmealDTO.setCreateUser(id);
        setmealDTO.setUpdateUser(id);
        return setmealService.edit(setmealDTO);
    }
}
